package goldeneagle.com.cart.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import goldeneagle.com.admin.dao.impl.AdminManagerDaoImpl;
import goldeneagle.com.entity.Cart;
import goldeneagle.com.entity.Product;

/**
 * Servlet implementation class CartServlet
 */
@WebServlet(value="/CartServlet")
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartServlet() {
        super();

    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("pid"));
//		String pageNum = request.getParameter("pageNum");
		AdminManagerDaoImpl pd = new AdminManagerDaoImpl();
		Product p = pd.findProductById(id);
		HttpSession session = request.getSession();
		Cart cart = (Cart)session.getAttribute("cart");
		if(cart==null) {	
			cart = new Cart();
		}
		cart.addItem(p);
		session.setAttribute("cart", cart);
		if(p.getPtype().equals("CPU")) {
			request.getRequestDispatcher("ProductList").forward(request, response);
		}else if(p.getPtype().equals("GPU")){
			request.getRequestDispatcher("ProductList").forward(request, response);
		}
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
